// Mirror class.
// Note: X and Y are the CENTER of the mirror's bounding box.
function Mirror(r) {
	
	// position.
	this.px = 0;
	this.py = 0;
	
	// rotation.
	this.rot = r;
	
	// mirrors are represented by a box. The length and
	// width are defined in terms of the half lengths.
	this.halfLength = 25;
	this.halfWidth = 3;
	
	// The bounding box will be a different color if the
	// mirror is currently selected.
	this.isSelected = false;
	
	// Says whether this object is currently active in the scene,
	// or whether it's available for placement.
	this.isPlaced = false;

	this.reflectsLaser = true;
	
	// render function for this mirror.
	Mirror.prototype.render = function(processing) {
		processing.pushMatrix();
		
		// translate to the screen position.
		processing.translate(this.px, this.py);
		
		// Draw the mirror's bounding box, which has no rotation information.
		processing.noFill();
		
		// Change the bounding box color if the mirror is selected.
		if (this.isSelected) {
			processing.stroke(0, 255, 0);
		} else if(this.isPlaced) {
			processing.noStroke();
		} else {
			processing.stroke(0, 0, 0);
		}
		
		processing.quad(
			-1 * this.halfLength, this.halfLength,
			this.halfLength, this.halfLength,
			this.halfLength, -1 * this.halfLength,
			-1 * this.halfLength, -1 * this.halfLength
		);
		
		// Draw the mirror itself, which is rotated.
		processing.rotate(processing.radians(this.rot));
				
		// Draw the mirror quad.
		processing.stroke(0,0,0);
		processing.fill(255, 255, 255);
		processing.quad(
			-1 * this.halfLength, this.halfWidth,
			this.halfLength, this.halfWidth,
			this.halfLength, -1 * this.halfWidth,
			-1 * this.halfLength, -1 * this.halfWidth);  
		processing.popMatrix();	
	}	
	
	Mirror.prototype.collides = function(x, y) {
		if (
			x >= (this.px - this.halfLength) &&
			x <= (this.px + this.halfLength) &&
			y >= (this.py - this.halfLength) &&
			y <= (this.py + this.halfLength)
			) {
			return true;
		}
		return false;
	}

	Mirror.prototype.emitsLaser = function() {
	  return false;
	}

	Mirror.prototype.update = function() {
	}

	Mirror.prototype.getInterruptDistance = function(vectorX, vectorY, vectorAngle) {
	  var x1 = vectorX;
	  var x2 = vectorX + Math.cos(Math.PI * vectorAngle / 180.0);
	  var y1 = vectorY;
	  var y2 = vectorY + Math.sin(Math.PI * vectorAngle / 180.0);
	  var x3 = this.px;
	  var x4 = this.px + Math.cos(Math.PI * this.rot / 180.0);
	  var y3 = this.py;
	  var y4 = this.py + Math.sin(Math.PI * this.rot / 180.0);
	  
	  var collisionXNumerator = (x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4);
	  var collisionXDenominator = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
	  if (collisionXDenominator == 0) {
	    return -1;
	  }
	  var collisionYNumerator = (x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4);
	  var collisionYDenominator = collisionXDenominator;

	  var collisionX = collisionXNumerator / collisionXDenominator;
	  var collisionY = collisionYNumerator / collisionYDenominator;

	  // Check if collision is inside the length of the mirror
	  var distanceToPaddle = Math.sqrt((collisionX - this.px) * (collisionX - this.px)
					   + (collisionY - this.py) * (collisionY - this.py));
	  if (distanceToPaddle > this.halfLength) {
	    return -1;
	  }

	  // Check if collision is in the right laser direction
	  if (x1 == x2) {
	    // check based on y
	    if ((collisionY - vectorY) / (y2 - y1) < 0) {
	      // In opposite directions
	      return -1;
	    }
	  } else {
	    if ((collisionX - vectorX) / (x2 - x1) < 0) {
	      return -1;
	    }
	  }

	  var distanceToOrigin = Math.sqrt((collisionX - vectorX) * (collisionX - vectorX)
					   + (collisionY - vectorY) * (collisionY - vectorY));
	  return distanceToOrigin;
	}

	Mirror.prototype.newLaserAngle = function(vectorX, vectorY, vectorAngle) {
	  normalizedAngleDiff = (vectorAngle - this.rot) % 360;
	  return this.rot - normalizedAngleDiff;
	}

	Mirror.prototype.handleLaser = function() {
	}

	Mirror.prototype.setPosition = function(x, y) {
		this.px = x;
		this.py = y;
	}
	
	Mirror.prototype.addRotation = function(additionalRotation) {
		this.rot = this.rot + additionalRotation;
	}
}
